package org.rain.common.classmeta;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * created by yangtong on 2025/4/15 16:51:42
 * <br/>
 * 方法元数据
 */
public class MethodMetadata {
    //方法对象
    private final Method method;
    //方法注解
    private final Map<Class<? extends Annotation>, Annotation> annotations;
    //方法参数
    private final List<ParameterMetadata> parameters;


    public MethodMetadata(Method method) {
        this.method = method;
        this.annotations = Arrays.stream(method.getAnnotations())
                .collect(Collectors.toMap(Annotation::annotationType, a -> a));

        this.parameters = new ArrayList<>();
        Class<?>[] types = method.getParameterTypes();
        Annotation[][] paramAnn = method.getParameterAnnotations();
        for (int i = 0; i < types.length; i++) {
            parameters.add(new ParameterMetadata(i, types[i], paramAnn[i]));
        }
    }

    public <A extends Annotation> A getAnnotation(Class<A> type) {
        return (A) annotations.get(type);
    }

    public boolean hasAnnotation(Class<? extends Annotation> type) {
        return annotations.containsKey(type);
    }
}
